import { getCommentAdopt, getCommentDelet, getcommentList, getCommentreply } from '@/services';
import { CommentLIst, ICommentAdopt, ICommentDelete, ICommentReply } from '@/types';
import { Effect, Reducer } from 'umi';

export interface CommentModelState {
  commentList: CommentLIst[];
  commentListlength:number;
  commentAdopt:Partial<ICommentAdopt>
  commentDelet:Partial<ICommentDelete>
  commentreply:Partial<ICommentReply>
  // articleDetail: Partial<ImailItem>; // 把接口的属性变成可选的
}

export interface CommentModelType {
  namespace: 'comment';
  state: CommentModelState;
  effects: {
    getcommentList: Effect;
    getCommentAdopt:Effect;
    getCommentDelet:Effect;
    getCommentreply:Effect;
  };
  reducers: {
    save: Reducer<CommentModelState>;
  };
}

const commentModel: CommentModelType = {
  // 数据模型，用来隔离数据防止相互影响
  namespace: 'comment',

  // 数据模型状态，相当于vuex的state
  state: {
    commentList: [],
    commentListlength:0,
    commentAdopt:{},
    commentDelet:{},
    commentreply:{}
  },

  // 异步操作，相当于vuex的action
  effects: {
    // 先简单记忆，*相当于async，yield相当于await
    *getcommentList({ payload }, { call, put }) {
      let result = yield call(getcommentList, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            commentList: result.data[0],
            commentListlength:result.data.lastItem
          }
        })
      }
    },
    //评论通过
    *getCommentAdopt({ payload }, { call, put }) {
      let result = yield call(getCommentAdopt,payload)
      if(result.data){
          yield put({
              type:"save",
              payload:{
                commentAdopt:result.data
              }
          })
      }
    },
    //评论通过
    *getCommentreply({ payload }, { call, put }) {
      let result = yield call(getCommentreply,payload)
      if(result.data){
          yield put({
              type:"save",
              payload:{
                commentreply:result.data
              }
          })
      }
    },
    *getCommentDelet({ payload }, { call, put }) {
      let result = yield call(getCommentDelet,payload)
      if(result.data){
          yield put({
              type:"save",
              payload:{
                commentDelet:result.data
              }
          })
      }
    },
  },

  // 同步操作，相当于vuex的mutation
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },

  }
};

export default commentModel;
